package org.dreamwork.jasmine2.web.client;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 12-4-25
 * Time: 下午4:15
 */
public class UserAgent {
    protected BrowserType type;
    protected DeviceType device;
    protected OperationSystem os;
    protected Version version;
    protected RenderKernel kernel;
    protected String rawString;

    protected static class UnknownUserAgent extends UserAgent {
        protected UnknownUserAgent (String rawString) {
            super (rawString);
            this.type = BrowserType.Unknown;
            this.device = DeviceType.Unknown;
            this.os = OperationSystem.UNKNOWN;
            this.version = Version.UNKNOWN;
            this.kernel = RenderKernel.UNKNOWN;
        }
    }

    UserAgent (String rawString) {
        this.rawString = rawString;
    }

    public UserAgent (HttpServletRequest request) {
        String expression = request.getHeader ("user-agent");
        UserAgent ua = new UserAgentParser ().parse (expression);
        type = ua.type;
        device = ua.device;
        os = ua.os;
        version = ua.version;
        kernel = ua.kernel;
    }

    public BrowserType getType () {
        return type;
    }

    public void setType (BrowserType type) {
        this.type = type;
    }

    public DeviceType getDevice () {
        return device;
    }

    public void setDevice (DeviceType device) {
        this.device = device;
    }

    public OperationSystem getOs () {
        return os;
    }

    public void setOs (OperationSystem os) {
        this.os = os;
    }

    public Version getVersion () {
        return version;
    }

    public void setVersion (Version version) {
        this.version = version;
    }

    public RenderKernel getKernel () {
        return kernel;
    }

    public void setKernel (RenderKernel kernel) {
        this.kernel = kernel;
    }

    public String getRawString () {
        return rawString;
    }

    public void setRawString (String rawString) {
        this.rawString = rawString;
    }

    @Override
    public String toString () {
        return "UserAgent{" +
                "type=" + type +
                ", kernel=" + kernel +
                ", device=" + device +
                ", os=" + os +
                ", version=" + version +
                '}';
    }
}
